package com.starsoft.quant.executor;

import java.util.Date;

public class StrategyRunningStatus {

	enum RunningStatus{
		Pedding,Running,Finish,Error
	}
	
	Integer acountId;
	Date beginDate;
 	Date endDate;
 	Date currentDate;
 	RunningStatus status;
 	
 	long startTime;
 	long finishTime;

 	public StrategyRunningStatus(Integer acountId){
 		this.acountId = acountId;
 		this.status=RunningStatus.Pedding;
 	}
 	
 	public void setDuration(Date beginDate, Date endDate){
 		this.beginDate = beginDate;
 		this.endDate = endDate;
 	}

	public int getPercent() {
		if (beginDate == null || endDate == null || currentDate == null) {
			return 0;
		}
		return (int) (100.0 * (currentDate.getTime() - beginDate.getTime()) / (endDate.getTime() - beginDate.getTime()));
	}
 	
	public String getStatusStr() {
		if (status == RunningStatus.Running) {
			return status.name() + " " + getPercent() + "%";
		} else if (status == RunningStatus.Finish){
			return status.name() + " " + (finishTime-startTime)/1000 + "s";
		} else {
			return status.name();
		}
	}
	
	public void beginRun(){
		if(status==RunningStatus.Pedding){
			status = RunningStatus.Running;
			startTime = new Date().getTime();
		} else {
			throw new RuntimeException("Wrong Status");
		}
	}
	
	public void finishRun(){
		if(status==RunningStatus.Running){
			status = RunningStatus.Finish;
			finishTime = new Date().getTime();
		} else {
			throw new RuntimeException("Wrong Status");
		}
	}
	
	public void errorRun() {
		status = RunningStatus.Error;
		finishTime = new Date().getTime();
	}

	public Integer getAcountId() {
		return acountId;
	}

	public void setAcountId(Integer acountId) {
		this.acountId = acountId;
	}

	public Date getBeginDate() {
		return beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public Date getCurrentDate() {
		return currentDate;
	}

	public void setCurrentDate(Date currentDate) {
		this.currentDate = currentDate;
	}

	public RunningStatus getStatus() {
		return status;
	}
 	
}
